package com.test.webtest.service.impl;

import com.test.webtest.entity.AuthorizationCertificate;
import com.test.webtest.service.AuthorizationCertificateService;
import org.springframework.stereotype.Service;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * @Description AuthorizationCertificateServiceImpl
 * @Author jiangzhenghao
 * @Date 2025-05-17  10:23
 */
@Service
public class AuthorizationCertificateServiceImpl implements AuthorizationCertificateService {

    @Override
    public String generateCertificate(AuthorizationCertificate certificate) throws IOException {
        // 模板图片路径
        String templatePath = "src/main/resources/templates/certificate_template.png";
        // 输出图片路径
        String outputPath = "src/main/resources/generated/certificate_" + System.currentTimeMillis() + ".png";

        // 读取模板图片
        BufferedImage templateImage = ImageIO.read(new File(templatePath));
        Graphics2D g2d = templateImage.createGraphics();

        // 设置字体样式
        Font font = new Font("宋体", Font.PLAIN, 15);
        // 字体颜色
        g2d.setColor(Color.BLACK);
        g2d.setFont(font);

        // 填充公司名称
        g2d.drawString(certificate.getCompanyName(), 300, 165);
        // 填充公司法人
        g2d.drawString(certificate.getName(), 155, 185);
        // 填充品牌名称
        g2d.drawString(certificate.getBrandName(), 305, 185);
        //填充图片,设置图片大小
        g2d.drawImage(ImageIO.read(new File("src/main/resources/templates/icon.png")), 50, 50, 100, 100, null);

        // 关闭绘图上下文
        g2d.dispose();

        // 保存生成的图片
        ImageIO.write(templateImage, "png", new File(outputPath));

        return outputPath;
    }
}
